Page({
  data: {
    orderId: '',
    order: {
      id: 1,
      status: 'paid',
      statusText: '待服务',
      statusDesc: '技师正在前往服务地点',
      statusProgress: 2,
      serviceImage: '/assets/images/service1.jpg',
      serviceName: '全身放松按摩',
      technicianName: '张技师',
      price: 198,
      appointmentTime: '2023-05-20 14:00',
      address: '北京市朝阳区某某街道某某小区1号楼101室',
      contactPerson: '李先生',
      contactPhone: '13800138000',
      remarks: '请准时到达',
      createTime: '2023-05-15 10:30:25',
      paidTime: '2023-05-15 10:35:12',
      actions: [
        { action: 'contact', text: '联系技师' },
        { action: 'cancel', text: '取消订单' }
      ]
    }
  },

  onLoad: function(options) {
    if (options.id) {
      this.setData({
        orderId: options.id
      });
      this.loadOrderDetail(options.id);
    }
  },

  loadOrderDetail: function(id) {
    const app = getApp();
    
    app.request({
      url: `${app.globalData.baseUrl}/orders/${id}/`,
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            order: res.data
          });
        }
      }
    });
  },

  handleAction: function(e) {
    const action = e.currentTarget.dataset.action;
    const orderId = e.currentTarget.dataset.id;
    
    switch(action) {
      case 'pay':
        this.payOrder(orderId);
        break;
      case 'cancel':
        this.cancelOrder(orderId);
        break;
      case 'contact':
        this.contactTechnician(orderId);
        break;
      case 'review':
        this.addReview(orderId);
        break;
    }
  },

  payOrder: function(orderId) {
    // 跳转到支付页面
    wx.navigateTo({
      url: `/pages/customer/payment?orderId=${orderId}`
    });
  },

  cancelOrder: function(orderId) {
    const app = getApp();
    
    wx.showModal({
      title: '确认取消',
      content: '确定要取消该订单吗？',
      success: (res) => {
        if (res.confirm) {
          app.request({
            url: `${app.globalData.baseUrl}/orders/${orderId}/cancel/`,
            method: 'POST',
            success: (res) => {
              if (res.statusCode === 200) {
                wx.showToast({
                  title: '取消成功',
                  icon: 'success'
                });
                // 重新加载订单详情
                this.loadOrderDetail(orderId);
              } else {
                wx.showToast({
                  title: '取消失败',
                  icon: 'none'
                });
              }
            }
          });
        }
      }
    });
  },

  contactTechnician: function(orderId) {
    // 联系技师功能
    wx.showToast({
      title: '联系技师功能',
      icon: 'none'
    });
  },

  addReview: function(orderId) {
    // 添加评价
    wx.navigateTo({
      url: `/pages/customer/addReview?orderId=${orderId}`
    });
  }
})